import { Args, Mutation, Parent, Query, ResolveProperty, Resolver } from "@nestjs/graphql";
import { Article } from "src/entities/article.entity";
import { Label } from "src/entities/label.entity";
import { User } from "src/entities/user.entity";
import { ArticleSortInput, PageInput, UserInput, UserSortInput, UserWhereInput } from "src/graphql";
import { ArticleService } from "src/services/article/article.service";
import { LabelService } from "src/services/label/label.service";
import { UserService } from "src/services/user/user.service";

@Resolver('User')
export class UserResolver {
  constructor(
    private readonly userService: UserService,
    private readonly articleService: ArticleService,
    private readonly labelService: LabelService,
  ) {}

  @Query('user')
  async getUserById(@Args() id: number) {
    return await this.userService.findOne(id);
  }

  @Query('users')
  async getUsers(@Args('page') page: PageInput,@Args('sort') sort: UserSortInput,@Args('where') where: UserWhereInput) {
    return await this.userService.getPagination(page,sort,where);
  }

  @Mutation()
  async addUser(@Args('user') user: UserInput) {
    return await this.userService.addOne(<User>user);
  }

  @Mutation()
  async removeUser(@Args() id: number) {
    return await this.userService.removeOne(id);
  }

  @Mutation()
  async modifyUser(@Args('id') id: number, @Args('user') user: UserInput) {
    return await this.userService.modifyOne(id,<User>user);
  }

  @ResolveProperty(() => Article,{
    name:'_articles'
  })
  async articles(@Parent() user){
    const { id } = user;
    return await this.articleService.findAll({ user_id: id });
  }

  @ResolveProperty(() => Label,{
    name:'_labels'
  })
  async labels(@Parent() user){
    const { id } = user;
    return await this.labelService.findAll({ user_id: id });
  }
}